# -*- coding: utf-8 -*-
# 主应用入口
# frontend/streamlit_app.py
import streamlit as st
import requests
import json
import time
from datetime import datetime
from components.chat_interface import ChatInterface
from components.model_selector import render_model_selector
from components.sidebar import render_sidebar
from utils.api_client import APIClient
from utils.session_manager import SessionManager
from styles.custom_css import load_custom_css

# 页面配置
st.set_page_config(
    page_title="🤖 智能模型群聊系统",
    page_icon="🤖",
    layout="wide",
    initial_sidebar_state="expanded"
)

# 加载自定义样式
load_custom_css()

# 初始化会话状态
if 'session_manager' not in st.session_state:
    st.session_state.session_manager = SessionManager()
if 'api_client' not in st.session_state:
    st.session_state.api_client = APIClient()
if 'messages' not in st.session_state:
    st.session_state.messages = []
if 'selected_models' not in st.session_state:
    st.session_state.selected_models = ['GPT-4']
if 'current_session_id' not in st.session_state:
    st.session_state.current_session_id = None

def main():
    """主应用函数"""
    
    # 顶部标题栏
    col1, col2, col3 = st.columns([2, 3, 1])
    
    with col1:
        st.markdown("## 🤖 智能模型群聊系统")
    
    with col2:
        st.markdown("### 💬 多模型实时对话平台")
    
    with col3:
        if st.button("🔄 刷新", key="refresh_btn"):
            st.rerun()
    
    # 创建主布局
    sidebar_col, main_col, details_col = st.columns([1, 2, 1])
    
    # 左侧边栏
    with sidebar_col:
        current_page = render_sidebar()
        st.session_state.current_page = current_page
    
    # 主聊天区域
    with main_col:
        chat_interface = ChatInterface()
        chat_interface.render()
    
    # 右侧详情栏
    with details_col:
        st.markdown("### 📊 模型详情")
        
        if st.session_state.selected_models:
            selected_model = st.selectbox(
                "选择查看模型",
                st.session_state.selected_models,
                key="detail_model_select"
            )
            
            # 模型信息卡片
            st.markdown(f"""
            <div style="
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                padding: 20px;
                border-radius: 10px;
                color: white;
                margin: 10px 0;
            ">
                <h3>🤖 {selected_model}</h3>
                <p>🔥 状态: 在线</p>
                <p>⚡ 响应时间: 2.3s</p>
                <p>💬 回复次数: 1,248</p>
            </div>
            """, unsafe_allow_html=True)
            
            # 模型配置
            st.markdown("#### ⚙️ 模型配置")
            temperature = st.slider("🌡️ 温度", 0.0, 1.0, 0.7, 0.1)
            max_tokens = st.slider("📝 最大长度", 100, 4000, 2000, 100)
            
            if st.button("💾 保存配置", key="save_config"):
                st.success("✅ 配置已保存！")

if __name__ == "__main__":
    main()